//
//  YYTopMenuView.m
//  NNHealthMall
//
//  Created by 蓓蕾 on 2021/8/20.
//  Copyright © 2021 YY. All rights reserved.
//

#import "YYTopMenuView.h"
#import "YYTopItemView.h"
#import <Masonry.h>
#import "TopMenuSelectView.h"



typedef void(^complete)(void);

#pragma mark - YYIndexPath
@implementation YYIndexPath

- (instancetype)initWithColumn:(NSInteger)column row:(NSInteger)row {
    if (self = [super init]) {
        _column = column;
        _row = row;
        _item = -1;
    }
    return self;
}

- (instancetype)initWithColumn:(NSInteger)column row:(NSInteger)row item:(NSInteger)item {
    self = [self initWithColumn:column row:row];
    if (self) {
        _item = item;
    }
    return self;
}

+ (instancetype)indexPathWithColumn:(NSInteger)column row:(NSInteger)row {
    YYIndexPath *path = [[self alloc] initWithColumn:column row:row];
    return path;
}

+ (instancetype)indexPathWithColumn:(NSInteger)column row:(NSInteger)row item:(NSInteger)item {
    return [[self alloc] initWithColumn:column row:row item:item];
}

@end

#pragma mark - YYTopMenuView
@interface YYTopMenuView () <UITableViewDataSource, UITableViewDelegate>
{
    struct {
        unsigned int numberOfRowsInColumn :1;
        unsigned int numberOfItemsInRow :1;
        unsigned int titleForRowsAtIndexPath :1;
        unsigned int titleForItemInRowAtIndexPath :1;
        unsigned int imageNameForRowAtIndexPath :1;
        unsigned int imageNameForItemInRowAtIndexPath :1;
        unsigned int detailTextForRowAtIndexPath :1;
        unsigned int detailTextForItemInRowAtIndexPath :1;
    }_dataSourceFlag;
}

@property (nonatomic, assign) CGFloat tableViewHeight;
@property (nonatomic, assign) CGPoint origin;  //原点
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) NSInteger numberOfColumn;  //列数
@property (nonatomic, assign) BOOL isShow;
@property (nonatomic, strong) UIView *backGroundView;
@property (nonatomic, strong) UITableView *leftTableView;  //一级列表
//@property (nonatomic, strong) UITableView *rightTableView;  //二级列表
@property (nonatomic, assign) NSInteger currentSelectedColumn;  //当前选中列
@property (nonatomic, strong) UIView *bottomLine;  //底部的线条

//layers array
@property (nonatomic, copy) NSArray *titles;
@property (nonatomic, copy) NSArray *indicators;
@property (nonatomic, copy) NSArray *bgLayers;

@property (nonatomic, strong) NSMutableArray *itemArr;
@property (nonatomic, strong) NSArray *normalImageArr;
@property (nonatomic, strong) NSArray *selectImageArr;
@property (nonatomic, assign) BOOL needCreateTopView;

@property (nonatomic, strong) TopMenuSelectView *selectView;

@end


@implementation YYTopMenuView

-(instancetype)initWithOrigin:(CGPoint)origin andDefaultColumnArr:(NSArray *)defaultColumnItemArr andNormalImageArr:(NSArray *)normalImageArr andSelectImageArr:(NSArray *)selectImageArr andHeight:(CGFloat)height
{
    if (self = [self initWithFrame:CGRectMake(origin.x, origin.y, YYMenu_SCREEN_WIDTH,height)]) {
        
        _leftMargin = -10;
        _topMargin = -10;
        _bottomMargin = -10;
        _itemHorizontalMargin = -10;
        _itemVerticalMargin = -10;
        _itemWidth = -10;
        _itemHeight = -10;
        _itemLineCount = -10;
        _showAnimation = YES;
        _allowShowSelectView = YES;
        
        
        _defaultColumnItemArr = defaultColumnItemArr;
        _normalImageArr = normalImageArr;
        _selectImageArr = selectImageArr;
        _needCreateTopView = YES;
            _origin = origin;
            _height = height;
            _isShow = NO;
            _fontSize = 14;
            _currentSelectedColumn = -1;
            _isClickHaveItemValid = YES;
            _textColor = YYMenu_kTextColor;
            _selectedTextColor = YYMenu_kTextSelectColor;
            _detailTextFont = [UIFont systemFontOfSize:11];
            _separatorColor = YYMenu_kSeparatorColor;
            _detailTextColor = YYMenu_kDetailTextColor;
            _indicatorColor = YYMenu_kTextColor;
            _tableViewHeight = YYMenu_kTableViewHeight;
            
            //初始化两个tableView
            _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x, origin.y + self.frame.size.height, YYMenu_SCREEN_WIDTH / 2, 0) style:UITableViewStylePlain];
            _leftTableView.delegate = self;
            _leftTableView.dataSource = self;
            _leftTableView.rowHeight = YYMenu_kTableViewCellHeight;
            _leftTableView.separatorColor = YYMenu_kSeparatorColor;
            
            self.backgroundColor = [UIColor whiteColor];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuTapped:)];
            [self addGestureRecognizer:tap];
            
            _backGroundView = [[UIView alloc] init];
            _backGroundView.frame = CGRectMake(origin.x, origin.y, YYMenu_SCREEN_WIDTH, YYMenu_SCREEN_HEIGHT);
            _backGroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
            _backGroundView.opaque = NO;
            UITapGestureRecognizer *backTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTapped:)];
            [_backGroundView addGestureRecognizer:backTap];
            
            //底部线条
            _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 0.5, YYMenu_SCREEN_WIDTH, 0.5)];
            _bottomLine.backgroundColor = YYMenu_kSeparatorColor;
            _bottomLine.hidden = YES;
            [self addSubview:_bottomLine];
        
        
    }
    return self;
}

-(void)setOrigin:(CGPoint)origin
{
    _origin = origin;
    self.frame = CGRectMake(origin.x, origin.y, YYMenu_SCREEN_WIDTH,_height);
    _backGroundView.frame = CGRectMake(origin.x, origin.y, YYMenu_SCREEN_WIDTH, YYMenu_SCREEN_HEIGHT);
    
    CGRect selectRect = _selectView.frame;
    selectRect = CGRectMake(_origin.x, _origin.y + self.height, CGRectGetWidth(selectRect), CGRectGetHeight(selectRect));
    
    _selectView.frame = selectRect;
}

-(void)initSelectView
{
    
    _selectView = [[TopMenuSelectView alloc] initWithFrame:CGRectMake(_origin.x, _origin.y + self.height, YYMenu_SCREEN_WIDTH,YYMenu_SCREEN_HEIGHT - _origin.y - self.height)];
    if (_fatherView) {
        [_fatherView addSubview:_selectView];
    }
    else
    {
        [self.superview addSubview:_selectView];
    }
    _selectView.showAnimation = _showAnimation;
    _selectView.leftMargin = _leftMargin;
    _selectView.itemLineCount = _itemLineCount;
    _selectView.itemWidth = _itemWidth;
    _selectView.topMargin = _topMargin;
    _selectView.bottomMargin = _bottomMargin;
    _selectView.itemHorizontalMargin = _itemHorizontalMargin;
    _selectView.itemVerticalMargin = _itemVerticalMargin;
    _selectView.itemHeight = _itemHeight;
    _selectView.autoSize = _autoSize;
    [_selectView setLeftTitleArr:_leftTitleArr withRightContentArr:_rightContentArr];
    YYMenuWeakSelf
    _selectView.SelectShaiXuanItemBlock = ^(NSInteger leftRow, NSInteger index) {
        if (weakSelf.SelectShaiXuanItemBlock) {
            weakSelf.SelectShaiXuanItemBlock(leftRow, index);
        }
    };
    _selectView.hidden = YES;
}

//初始化方法
- (instancetype)initWithOrigin:(CGPoint)origin andHeight:(CGFloat)height {
    self = [self initWithFrame:CGRectMake(origin.x, origin.y, YYMenu_SCREEN_WIDTH, height)];
    if (self) {
        _origin = origin;
        _height = height;
        _isShow = NO;
        _fontSize = 14;
        _currentSelectedColumn = -1;
        _isClickHaveItemValid = YES;
        _textColor = YYMenu_kTextColor;
        _selectedTextColor = YYMenu_kTextSelectColor;
        _detailTextFont = [UIFont systemFontOfSize:11];
        _separatorColor = YYMenu_kSeparatorColor;
        _detailTextColor = YYMenu_kDetailTextColor;
        _indicatorColor = YYMenu_kTextColor;
        _tableViewHeight = YYMenu_kTableViewHeight;
        
        //初始化两个tableView
        _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x, origin.y + self.frame.size.height, YYMenu_SCREEN_WIDTH / 2, 0) style:UITableViewStylePlain];
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
        _leftTableView.rowHeight = YYMenu_kTableViewCellHeight;
        _leftTableView.separatorColor = YYMenu_kSeparatorColor;
        
        self.backgroundColor = [UIColor whiteColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuTapped:)];
        [self addGestureRecognizer:tap];
        
        _backGroundView = [[UIView alloc] init];
        _backGroundView.frame = CGRectMake(origin.x, origin.y, YYMenu_SCREEN_WIDTH, YYMenu_SCREEN_HEIGHT);
        _backGroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
        _backGroundView.opaque = NO;
        UITapGestureRecognizer *backTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTapped:)];
        [_backGroundView addGestureRecognizer:backTap];
        
        //底部线条
        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 0.5, YYMenu_SCREEN_WIDTH, 0.5)];
        _bottomLine.backgroundColor = YYMenu_kSeparatorColor;
        _bottomLine.hidden = YES;
        [self addSubview:_bottomLine];
        
    }
    return self;
}

#pragma mark - 懒加载
- (UIColor *)indicatorColor {
    if (!_indicatorColor) {
        _indicatorColor = [UIColor blackColor];
    }
    return _indicatorColor;
}

- (UIColor *)textColor {
    if (!_textColor) {
        _textColor = [UIColor blackColor];
    }
    return _textColor;
}

- (UIColor *)separatorColor {
    if (!_separatorColor) {
        _separatorColor = [UIColor blackColor];
    }
    return _separatorColor;
}

#pragma mark - 设置dataSource
- (void)setDataSource:(id<YYMenuDataSource>)dataSource {
    if (_dataSource == dataSource) {
        return;
    }
    _dataSource = dataSource;
    if ([_dataSource respondsToSelector:@selector(numberOfColumnsInMenu:)]) {
        _numberOfColumn = [_dataSource numberOfColumnsInMenu:self];
    }else {
        _numberOfColumn = 1;
    }
    
    
    _currentSelectedRows = [[NSMutableArray alloc] initWithCapacity:_numberOfColumn];
    for (int i=0; i<_numberOfColumn; i++) {
        [_currentSelectedRows addObject:@(0)];
    }
    
    //判断是否响应了某方法
    _dataSourceFlag.numberOfRowsInColumn = [_dataSource respondsToSelector:@selector(menu:numberOfRowsInColumn:)];
    _dataSourceFlag.numberOfItemsInRow = [_dataSource respondsToSelector:@selector(menu:numberOfItemsInRow:inColumn:)];
    _dataSourceFlag.titleForRowsAtIndexPath = [_dataSource respondsToSelector:@selector(menu:titleForRowAtIndexPath:)];
    _dataSourceFlag.titleForItemInRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:titleForItemsInRowAtIndexPath:)];
    _dataSourceFlag.imageNameForRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:imageNameForRowAtIndexPath:)];
    _dataSourceFlag.imageNameForItemInRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:imageForItemsInRowAtIndexPath:)];
    _dataSourceFlag.detailTextForRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:detailTextForRowAtIndexPath:)];
    _dataSourceFlag.detailTextForItemInRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:detailTextForItemsInRowAtIndexPath:)];
    
    CGFloat numberOfLine = YYMenu_SCREEN_WIDTH / self.numberOfColumn;
    CGFloat numberOfBackground = YYMenu_SCREEN_WIDTH / self.numberOfColumn;
    CGFloat numberOfTextLayer = YYMenu_SCREEN_WIDTH / (self.numberOfColumn * 2);
    
    #pragma mark - 底部的line显示
    _bottomLine.hidden = NO;
    
    NSMutableArray *tempTitles = [[NSMutableArray alloc] initWithCapacity:_numberOfColumn];
    NSMutableArray *tempIndicators = [[NSMutableArray alloc] initWithCapacity:_numberOfColumn];
    NSMutableArray *tempBgLayers = [[NSMutableArray alloc] initWithCapacity:_numberOfColumn];
    if (_needCreateTopView) {
        _itemArr = [NSMutableArray new];
        CGFloat width = YYMenu_SCREEN_WIDTH / _numberOfColumn;
        UIView *lastView = nil;
        for (NSInteger i = 0; i < _numberOfColumn; ++i) {
            UIView *itemBgView = [[UIView alloc] init];
            itemBgView.tag = i + 1;
            [self addSubview:itemBgView];
            if (lastView) {
                [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(lastView.mas_right).mas_offset(0);
                    make.top.mas_equalTo(self.mas_top).mas_offset(0);
                    make.bottom.mas_equalTo(self.mas_bottom).mas_offset(0);
                    make.width.mas_equalTo(width);
                }];
            }
            else
            {
                [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(self.mas_left).mas_offset(0);
                    make.top.mas_equalTo(self.mas_top).mas_offset(0);
                    make.bottom.mas_equalTo(self.mas_bottom).mas_offset(0);
                    make.width.mas_equalTo(width);
                }];
            }
            
            YYTopItemView *itemView = [[YYTopItemView alloc] init];
            [itemBgView addSubview:itemView];
            itemView.tag = 10000;
//            itemView.backgroundColor = [UIColor greenColor];
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(itemBgView.mas_right).mas_offset(0);
                make.top.mas_equalTo(itemBgView.mas_top).mas_offset(0);
                make.bottom.mas_equalTo(itemBgView.mas_bottom).mas_offset(0);
                make.width.mas_lessThanOrEqualTo(itemBgView).priorityLow();
                make.width.mas_lessThanOrEqualTo(itemBgView);
            }];
            if (i < _defaultColumnItemArr.count) {
                NSString *defaultColumnItemTitle = _defaultColumnItemArr[i];
                itemView.titleString = defaultColumnItemTitle;
                if (i < _normalImageArr.count) {
                    NSString *normalImageName = _normalImageArr[i];
                    if ([normalImageName isKindOfClass:[NSString class]] && normalImageName.length > 0) {
                        itemView.normalIconString = normalImageName;
                        itemView.imageSize = CGSizeMake(20, 20);
                        itemView.itemMargin = 0;
                    }
                }
                if (i < _selectImageArr.count) {
                    NSString *selectImageName = _selectImageArr[i];
                    if ([selectImageName isKindOfClass:[NSString class]] && selectImageName.length > 0) {
                        itemView.selectIconString = selectImageName;
                    }
                }
            }
            [_itemArr addObject:itemView];
            if (i == _numberOfColumn - 1) {
                itemView.allowSelected = NO;
            }
            lastView = itemBgView;
        }
        return;
    }
    
    
    #pragma mark - 画出菜单
    for (int i = 0; i < _numberOfColumn; i++) {
        //backgrounLayer
        CGPoint positionForBackgroundLayer = CGPointMake((i + 0.5) * numberOfBackground, self.height / 2);
        CALayer *bgLayer = [self createBackgroundLayerWithPosition:positionForBackgroundLayer color:[UIColor whiteColor]];
        [self.layer addSublayer:bgLayer];
        [tempBgLayers addObject:bgLayer];
        
        //titleLayer
        NSString *titleString = nil;
        if (!_isClickHaveItemValid && [_dataSource menu:self numberOfItemsInRow:0 inColumn:i] > 0 && _dataSourceFlag.numberOfItemsInRow) {
            titleString = [_dataSource menu:self titleForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:i row:0 item:0]];
        }else {
            titleString = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:i row:0]];
        }
        if (i < _defaultColumnItemArr.count) {
            NSString *defaultTitleString = _defaultColumnItemArr[i];
            if ([defaultTitleString isKindOfClass:[NSString class]] && defaultTitleString.length > 0) {
                titleString = defaultTitleString;
            }
        }
//        NSLog(@"titleString = %@",titleString);
        CGPoint positionForTitle = CGPointMake(( i * 2 + 1) * numberOfTextLayer, self.height / 2);
        CATextLayer *textLayer = [self createTitleLayerWithString:titleString position:positionForTitle color:self.textColor];
        [self.layer addSublayer:textLayer];
        [tempTitles addObject:textLayer];
        
        
        //indicatorLayer
        CGSize size = [self calculateTitleSizeWithString:titleString];
        CGPoint indicatorPosition = CGPointMake(textLayer.position.x+size.width/2+10, self.height / 2+2);
        CAShapeLayer *sharpLayer = [self createIndicatorWithPosition:indicatorPosition color:self.indicatorColor];
        [self.layer addSublayer:sharpLayer];
        [tempIndicators addObject:sharpLayer];
        
        //separatorLayer
        if (i != self.numberOfColumn - 1) {
            CGPoint separatorPosition = CGPointMake(ceilf((i + 1) * numberOfLine - 1), self.height / 2);
            CAShapeLayer *separatorLayer = [self createSeparatorWithPosition:separatorPosition color:self.separatorColor];
            [self.layer addSublayer:separatorLayer];
        }
    }
    _titles = [tempTitles copy];
    _indicators = [tempIndicators copy];
    _bgLayers = [tempBgLayers copy];
}

#pragma mark - 绘图
#pragma mark - 背景
- (CALayer *)createBackgroundLayerWithPosition:(CGPoint)position color:(UIColor *)color {
    CALayer *layer = [CALayer layer];
    layer.position = position;
    layer.bounds = CGRectMake(0, 0, YYMenu_SCREEN_WIDTH / self.numberOfColumn, self.height - 1);
    layer.backgroundColor = color.CGColor;
    return layer;
}
#pragma mark - 标题
- (CATextLayer *)createTitleLayerWithString:(NSString *)string position:(CGPoint)position color:(UIColor *)color {
    CGSize size = [self calculateTitleSizeWithString:string];
    
    CATextLayer *layer = [CATextLayer new];
    CGFloat sizeWidth = (size.width < (self.frame.size.width / _numberOfColumn) - 25) ? size.width : self.frame.size.width / _numberOfColumn - 25;
    layer.bounds = CGRectMake(0, 0, sizeWidth, size.height);
    layer.string = string;
    layer.fontSize = _fontSize;
    layer.alignmentMode = kCAAlignmentCenter;
    layer.truncationMode = kCATruncationEnd;
    layer.foregroundColor = color.CGColor;
    
    layer.contentsScale = [[UIScreen mainScreen] scale];
    
    layer.position = position;
    
    return layer;
}
#pragma mark - 计算String的宽度
- (CGSize)calculateTitleSizeWithString:(NSString *)string
{
    //CGFloat fontSize = 14.0;
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:_fontSize]};
    CGSize size = [string boundingRectWithSize:CGSizeMake(280, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
    return CGSizeMake(ceilf(size.width)+2, size.height);
}

#pragma mark - 指示器
- (CAShapeLayer *)createIndicatorWithPosition:(CGPoint)position color:(UIColor *)color {
    CAShapeLayer *layer = [CAShapeLayer new];
    
    UIBezierPath *path = [UIBezierPath new];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(5, 5)];
    [path moveToPoint:CGPointMake(5, 5)];
    [path addLineToPoint:CGPointMake(10, 0)];
    [path closePath];
    layer.path = path.CGPath;
    layer.lineWidth = 0.8;
    layer.strokeColor = [UIColor blackColor].CGColor;
//    layer.fillColor = self.selectedTextColor.CGColor;
    
    
    CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit);
    layer.bounds = CGPathGetBoundingBox(bound);
    CGPathRelease(bound);
    layer.position = position;
    
    return layer;
}
#pragma mark - 分隔线
- (CAShapeLayer *)createSeparatorWithPosition:(CGPoint)position color:(UIColor *)color {
    CAShapeLayer *layer = [CAShapeLayer new];
    
//    UIBezierPath *path = [UIBezierPath new];
//    [path moveToPoint:CGPointMake(160,0)];
//    [path addLineToPoint:CGPointMake(160, 20)];
//
//    layer.path = path.CGPath;
//    layer.lineWidth = 1;
//    layer.strokeColor = color.CGColor;
//
//
//    CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit);
//    layer.bounds = CGPathGetBoundingBox(bound);
//    CGPathRelease(bound);
//    layer.position = position;
    return layer;
}

#pragma mark - 动画
- (void)animateIndicator:(CAShapeLayer *)indicator reverse:(BOOL)reverse complete:(complete)complete {
    [CATransaction begin];
    [CATransaction setAnimationDuration:0.25];
    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0.4 :0.0 :0.2 :1.0]];
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    anim.values = reverse ? @[ @0, @(M_PI) ] : @[ @(M_PI), @0 ];
    
    if (!anim.removedOnCompletion) {
        [indicator addAnimation:anim forKey:anim.keyPath];
    } else {
        [indicator addAnimation:anim forKey:anim.keyPath];
        [indicator setValue:anim.values.lastObject forKeyPath:anim.keyPath];
    }
    
    [CATransaction commit];
    if (reverse) {
        indicator.strokeColor = self.selectedTextColor.CGColor;
    }else {
        indicator.strokeColor = self.textColor.CGColor;
    }
    complete();
}

- (void)animateBackGroundView:(UIView *)view show:(BOOL)show complete:(complete)complete {
    if (show) {
        if (_fatherView) {
            [_fatherView addSubview:view];
        }
        else
        {
            [self.superview addSubview:view];
        }
        [view.superview addSubview:self];
        [UIView animateWithDuration:0.2 animations:^{
            view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        }];
    }else {
        [UIView animateWithDuration:0.2 animations:^{
            view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
        } completion:^(BOOL finished) {
            if (finished) {
                [view removeFromSuperview];
            }
        }];
    }
    complete();
}

- (void)animateTableView:(UITableView *)tableview show:(BOOL)show complete:(complete)complete {
    BOOL haveItems = NO;
    if (_dataSource) {
        NSInteger num = [self.leftTableView numberOfRowsInSection:0];
        for (int i=0; i<num; i++) {
            if (_dataSourceFlag.numberOfItemsInRow && [_dataSource menu:self numberOfItemsInRow:i inColumn:self.currentSelectedColumn] > 0) {
                haveItems = YES;
                break;
            }
        }
    }
    YYMenuWeakSelf
    if (show) {
        if (haveItems) {
            _leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH / 2, 0);
            if (_fatherView) {
                [_fatherView addSubview:_leftTableView];
            }
            else
            {
                [self.superview addSubview:_leftTableView];
            }
        }else {
            _leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH, 0);
            if (_fatherView) {
                [_fatherView addSubview:_leftTableView];
            }
            else
            {
                [self.superview addSubview:_leftTableView];
            }
        }
        CGFloat num = [_leftTableView numberOfRowsInSection:0];
        CGFloat tableViewHeight = num * YYMenu_kTableViewCellHeight > YYMenu_kTableViewHeight ? YYMenu_kTableViewHeight : num * YYMenu_kTableViewCellHeight;
        
        [UIView animateWithDuration:0.2 animations:^{
            if (haveItems) {
                weakSelf.leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH / 2, tableViewHeight);
//                weakSelf.rightTableView.frame = CGRectMake(self.origin.x + YYMenu_SCREEN_WIDTH / 2, self.origin.y +self.height, YYMenu_SCREEN_WIDTH / 2, tableViewHeight);
            }else {
                weakSelf.leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH, tableViewHeight);
            }
        }];
    }else {
        [UIView animateWithDuration:0.2 animations:^{
            if (haveItems) {
                weakSelf.leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH / 2, 0);
//                weakSelf.rightTableView.frame = CGRectMake(self.origin.x + YYMenu_SCREEN_WIDTH / 2, self.origin.y +self.height, YYMenu_SCREEN_WIDTH / 2, 0);
            }else {
                weakSelf.leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH, 0);
            }
        } completion:^(BOOL finished) {
//            if (weakSelf.rightTableView) {
//                [weakSelf.rightTableView removeFromSuperview];
//            }
            [weakSelf.leftTableView removeFromSuperview];
        }];
    }
    complete();
}

- (void)animateTitle:(CATextLayer *)title Indicator:(CAShapeLayer *)indicator show:(BOOL)show complete:(complete)complete {
    CGSize size = [self calculateTitleSizeWithString:title.string];
    CGFloat sizeWidth = (size.width < (self.frame.size.width / _numberOfColumn) - 25) ? size.width : self.frame.size.width / _numberOfColumn - 25;
    title.bounds = CGRectMake(0, 0, sizeWidth, size.height);
    if (indicator) {
        CGPoint indicatorPosition = CGPointMake(title.position.x+sizeWidth/2+10, self.height / 2+2);
        indicator.position = indicatorPosition;
    }
    if (!show) {
        title.foregroundColor = _textColor.CGColor;
    } else {
        title.foregroundColor = _selectedTextColor.CGColor;
    }
    complete();
}

- (void)animateIndicator:(CAShapeLayer *)indicator background:(UIView *)background tableView:(UITableView *)tableView title:(CATextLayer *)title reverse:(BOOL)reverse complecte:(void(^)(void))complete {
    
    [self animateIndicator:indicator reverse:reverse complete:^{
       [self animateTitle:title Indicator:indicator show:reverse complete:^{
          [self animateBackGroundView:background show:reverse complete:^{
             [self animateTableView:tableView show:reverse complete:^{
                 
             }];
          }];
       }];
    }];
    complete();
}


#pragma mark - UITableView的dataSource和delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _leftTableView) {
        if (_dataSourceFlag.numberOfRowsInColumn) {
            return [_dataSource menu:self numberOfRowsInColumn:_currentSelectedColumn];
        }else {
            return 0;
        }
    }else {
        if (_dataSourceFlag.numberOfItemsInRow) {
            NSInteger row = [_currentSelectedRows[_currentSelectedColumn] integerValue];
            return [_dataSource menu:self numberOfItemsInRow:row inColumn:_currentSelectedColumn];
        }else {
            return 0;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        cell.textLabel.textColor = _textColor;
        cell.textLabel.highlightedTextColor = _selectedTextColor;
        cell.textLabel.font = [UIFont systemFontOfSize:_fontSize];
        if (_dataSourceFlag.detailTextForRowAtIndexPath && _dataSourceFlag.detailTextForItemInRowAtIndexPath) {
            cell.detailTextLabel.textColor = _detailTextColor;
            cell.detailTextLabel.font = _detailTextFont;
        }
    }
    if (tableView == _leftTableView) {
        if (_dataSourceFlag.titleForRowsAtIndexPath) {
            cell.textLabel.text = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row]];
            if (_dataSourceFlag.imageNameForRowAtIndexPath) {
                NSString *imgName = [_dataSource menu:self imageNameForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row]];
                if (imgName && imgName.length > 0) {
                    cell.imageView.image = [UIImage imageNamed:imgName];
                }else {
                    cell.imageView.image = nil;
                }
            }else {
                cell.imageView.image = nil;
            }
            
            //detailText
            if (_dataSourceFlag.detailTextForRowAtIndexPath) {
                cell.detailTextLabel.text = [_dataSource menu:self detailTextForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row]];
            }else {
                cell.detailTextLabel.text = nil;
            }
        }
        //设置accessory
        NSInteger currentSelectRow = [_currentSelectedRows[_currentSelectedColumn] integerValue];
        
        //NSLog(@"当前%ld栏中%ld列", _currentSelectedColumn, currentSelectRow);
        
        if (indexPath.row == currentSelectRow) {
            [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
        
        if (_dataSourceFlag.numberOfItemsInRow && [_dataSource menu:self numberOfItemsInRow:indexPath.row inColumn:_currentSelectedColumn] > 0) {
            cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory_normal"] highlightedImage:[UIImage imageNamed:@"accessory_highlight"]];
        }else {
            cell.accessoryView = nil;
        }
    }else {
        //右边
        if (_dataSourceFlag.titleForItemInRowAtIndexPath) {
            NSInteger currentSelectedRow = [_currentSelectedRows[_currentSelectedColumn] integerValue];
            cell.textLabel.text = [_dataSource menu:self titleForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:currentSelectedRow item:indexPath.row]];
            if (_dataSourceFlag.imageNameForItemInRowAtIndexPath) {
                NSString *imgName = [_dataSource menu:self imageForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:currentSelectedRow item:indexPath.row]];
                if (imgName && imgName.length > 0) {
                    cell.imageView.image = [UIImage imageNamed:imgName];
                }else {
                    cell.imageView.image = nil;
                }
            }else {
                cell.imageView.image = nil;
            }
            
            if (_dataSourceFlag.detailTextForItemInRowAtIndexPath) {
                cell.detailTextLabel.text = [_dataSource menu:self detailTextForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:currentSelectedRow item:indexPath.row]];
            }else {
                cell.textLabel.text = nil;
            }
        }
        if ([cell.textLabel.text isEqualToString:[(CATextLayer *)_titles[_currentSelectedColumn] string]]) {
            NSInteger currentSelectedRow = [_currentSelectedRows[_currentSelectedColumn] integerValue];
            [_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentSelectedRow inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//            [_rightTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        }
        
        cell.accessoryView = nil;
        
    }
    return cell;
}

-(void)selectItemWithIndexPath:(YYIndexPath *)indexPath
{
    if (_needCreateTopView) {
//        if (self.SelectIndexPathBlock) {
//            YYIndexPath *currentSelectIndexPath = [YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row];
//            self.SelectIndexPathBlock(currentSelectIndexPath);
//        }
        
        
        
        UIView *view = [self viewWithTag:indexPath.column + 1];
        YYTopItemView *itemView = [view viewWithTag:10000];
        if ([itemView isKindOfClass:[YYTopItemView class]]) {
            NSString *itemTitleString = _defaultColumnItemArr[indexPath.column];
            
        NSString *selectString = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:indexPath.column row:indexPath.row]];
            
            NSLog(@"itemTitleString = %@",itemTitleString);
            NSLog(@"selectString = %@",selectString);
            if (indexPath.row == 0) {
                itemView.titleString = _defaultColumnItemArr[indexPath.column];
            }
            else
            {
                itemView.titleString = selectString;
            }
            
            [self backTapped:nil];
            
        }
        return;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_needCreateTopView) {
        if (self.SelectIndexPathBlock) {
            YYIndexPath *currentSelectIndexPath = [YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row];
            self.SelectIndexPathBlock(currentSelectIndexPath);
        }
        
        
        
        UIView *view = [self viewWithTag:_currentSelectedColumn + 1];
        YYTopItemView *itemView = [view viewWithTag:10000];
        if ([itemView isKindOfClass:[YYTopItemView class]]) {
            NSString *itemTitleString = _defaultColumnItemArr[_currentSelectedColumn];
            
        NSString *selectString = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row]];
            
            NSLog(@"itemTitleString = %@",itemTitleString);
            NSLog(@"selectString = %@",selectString);
            if (indexPath.row == 0) {
                itemView.titleString = _defaultColumnItemArr[_currentSelectedColumn];
            }
            else
            {
                itemView.titleString = selectString;
            }
            
            [self backTapped:nil];
            
        }
        return;
    }
        BOOL haveItem = [self setMenuWithSelectedRow:indexPath.row];
        BOOL isClickHaveItemValid = self.isClickHaveItemValid ? YES :haveItem;
        if (isClickHaveItemValid && _delegate &&[_delegate respondsToSelector:@selector(menu:didSelectRowAtIndexPath:)]) {
            [_delegate menu:self didSelectRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row]];
        }
}

#pragma mark 解决cell分割线左侧留空的问题
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 下面这几行代码是用来设置cell的上下行线的位置
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    //按照作者最后的意思还要加上下面这一段，才能做到底部线控制位置，所以这里按stackflow上的做法添加上吧。
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

#pragma mark - 方法实现
//默认选中的index
- (void)selectDeafultIndexPath {
    [self selectIndexPath:[YYIndexPath indexPathWithColumn:0 row:0]];
}
//获取IndexPath所对应的字符串
- (NSString *)titleForRowAtIndexPath:(YYIndexPath *)indexPath {
    return [self.dataSource menu:self titleForRowAtIndexPath:indexPath];
}
//菜单切换
- (void)selectIndexPath:(YYIndexPath *)indexPath {
    if (!_delegate || !_dataSource || ![_delegate respondsToSelector:@selector(menu:didSelectRowAtIndexPath:)]) {
        return;
    }
    
    if ([_dataSource numberOfColumnsInMenu:self] <= indexPath.column || [_dataSource menu:self numberOfRowsInColumn:indexPath.column] <= indexPath.row) {
        return;
    }
    
    CATextLayer *title = (CATextLayer *)_titles[indexPath.column];
    
    if (indexPath.item < 0 ) {
        
        if (!_isClickHaveItemValid && [_dataSource menu:self numberOfItemsInRow:indexPath.row inColumn:indexPath.column] > 0){
            title.string = [_dataSource menu:self titleForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:indexPath.column row:indexPath.row]];
                [_delegate menu:self didSelectRowAtIndexPath:[YYIndexPath indexPathWithColumn:indexPath.column row:indexPath.row item:0]];
        }else {
            title.string = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:indexPath.column row:indexPath.row]];
            [_delegate menu:self didSelectRowAtIndexPath:indexPath];
        }
        if (_currentSelectedRows.count > indexPath.column) {
            _currentSelectedRows[indexPath.column] = @(indexPath.row);
        }
        CGSize size = [self calculateTitleSizeWithString:title.string];
        CGFloat sizeWidth = (size.width < (self.frame.size.width / _numberOfColumn) - 25) ? size.width : self.frame.size.width / _numberOfColumn - 25;
        title.bounds = CGRectMake(0, 0, sizeWidth, size.height);
    }else if ([_dataSource menu:self numberOfItemsInRow:indexPath.row inColumn:indexPath.column] > indexPath.column) {
        title.string = [_dataSource menu:self titleForItemsInRowAtIndexPath:indexPath];
        [_delegate menu:self didSelectRowAtIndexPath:indexPath];
        if (_currentSelectedRows.count > indexPath.column) {
            _currentSelectedRows[indexPath.column] = @(indexPath.row);
        }
        CGSize size = [self calculateTitleSizeWithString:title.string];
        CGFloat sizeWidth = (size.width < (self.frame.size.width / _numberOfColumn) - 25) ? size.width : self.frame.size.width / _numberOfColumn - 25;
        title.bounds = CGRectMake(0, 0, sizeWidth, size.height);
    }
    
}
#pragma mark - 数据重载
- (void)reloadData {
    YYMenuWeakSelf
    [self animateBackGroundView:_backGroundView show:NO complete:^{
       [self animateTableView:nil show:NO complete:^{
           weakSelf.isShow = NO;
           id vc = self.dataSource;
           self.dataSource = nil;
           self.dataSource = vc;
       }];
    }];
}

-(void)ShaiXuanMethod
{
    if (_selectView.hidden) {
        self.shaiXuanViewHidden = NO;
        [self showSelectView];
        [self backTapped:nil];
    }
    else
    {
        self.shaiXuanViewHidden = YES;
        [self hiddenSelectView];
    }
}

-(void)setShaiXuanViewHidden:(BOOL)shaiXuanViewHidden
{
    _shaiXuanViewHidden = shaiXuanViewHidden;
    _selectView.hidden = shaiXuanViewHidden;
}

-(void)showColumnView:(NSInteger)column
{
    if (_dataSource == nil) {
        return;
    }
    
    NSInteger touchIndex = column;
    YYMenuWeakSelf
    if (_needCreateTopView) {
        UIView *view = [self viewWithTag:touchIndex + 1];
        YYTopItemView *itemView = [view viewWithTag:10000];
        if ([itemView isKindOfClass:[YYTopItemView class]]) {
            itemView.selected = !itemView.selected;
            for (YYTopItemView *objectView in _itemArr) {
                if (objectView != itemView) {
                    objectView.selected = NO;
                }
            }
        }
    }
//    return;
    if (touchIndex == _numberOfColumn - 1) {
        if (_selectView.hidden) {
            [self showSelectView];
            [self backTapped:nil];
        }
        else
        {
            [self hiddenSelectView];
        }
        return;
    }
    else
    {
        [self hiddenSelectView];
    }
    
    for (int i=0; i<_numberOfColumn; i++) {
        if (i != touchIndex) {
            [self animateIndicator:_indicators[i] reverse:NO complete:^{
               [self animateTitle:weakSelf.titles[i] Indicator:nil show:NO complete:^{
                   
               }];
            }];
        }
    }
    
    if (touchIndex == _currentSelectedColumn && _isShow) {
        //收回menu
        [self animateIndicator:_indicators[touchIndex] background:_backGroundView tableView:_leftTableView title:_titles[touchIndex] reverse:NO complecte:^{
            weakSelf.currentSelectedColumn = touchIndex;
            weakSelf.isShow = NO;
        }];
    }else {
        //弹出menu
        _currentSelectedColumn = touchIndex;
        [_leftTableView reloadData];
//        if (_dataSource && _dataSourceFlag.numberOfItemsInRow) {
////            [_rightTableView reloadData];
//        }
        [self animateIndicator:_indicators[touchIndex] background:_backGroundView tableView:_leftTableView title:_titles[touchIndex] reverse:YES complecte:^{
            weakSelf.isShow = YES;
        }];
        
        
        if (self.GetCurrenSelectColumnBlock) {
            self.GetCurrenSelectColumnBlock(touchIndex, YES);
        }
    }
}

-(void)selectShaiXuanItemWithLeftRow:(NSInteger)row withIndex:(NSInteger)index
{
    [_selectView selectShaiXuanItemWithLeftRow:row withIndex:index];
}

- (void)menuTapped:(UITapGestureRecognizer *)gesture {
    if (_dataSource == nil) {
        return;
    }
    //触摸的地方的index
    CGPoint touchPoint = [gesture locationInView:self];
    NSInteger touchIndex = touchPoint.x / (YYMenu_SCREEN_WIDTH / self.numberOfColumn);
    YYMenuWeakSelf
    //将当前点击的column之外的column给收回
//    UIView *gestureView = gesture.view;
    
    if (_needCreateTopView) {
        UIView *view = [self viewWithTag:touchIndex + 1];
        YYTopItemView *itemView = [view viewWithTag:10000];
        if ([itemView isKindOfClass:[YYTopItemView class]]) {
            itemView.selected = !itemView.selected;
            for (YYTopItemView *objectView in _itemArr) {
                if (objectView != itemView) {
                    objectView.selected = NO;
                }
            }
        }
    }
//    return;
    if (touchIndex == _numberOfColumn - 1) {
        NSLog(@"点击了最后一个");
        if (_selectView.hidden) {
            self.shaiXuanViewHidden = NO;
            [self showSelectView];
            [self backTapped:nil];
            if (self.SelectShaiXuanBlock) {
                NSLog(@"筛选回调");
                self.SelectShaiXuanBlock();
            }
        }
        else
        {
            self.shaiXuanViewHidden = YES;
            [self hiddenSelectView];
            if (self.HiddenShaiXuanViewBlock) {
                self.HiddenShaiXuanViewBlock();
            }
        }
        if (self.ShaiXuanBlock) {
            self.ShaiXuanBlock();
        }
        return;
    }
    else
    {
        self.shaiXuanViewHidden = YES;
        [self hiddenSelectView];
    }
    
    for (int i=0; i<_numberOfColumn; i++) {
        if (i != touchIndex) {
            [self animateIndicator:_indicators[i] reverse:NO complete:^{
               [self animateTitle:weakSelf.titles[i] Indicator:nil show:NO complete:^{
                   
               }];
            }];
        }
    }
    
    if (touchIndex == _currentSelectedColumn && _isShow) {
        //收回menu
        [self animateIndicator:_indicators[touchIndex] background:_backGroundView tableView:_leftTableView title:_titles[touchIndex] reverse:NO complecte:^{
            weakSelf.currentSelectedColumn = touchIndex;
            weakSelf.isShow = NO;
        }];
    }else {
        //弹出menu
        _currentSelectedColumn = touchIndex;
        [_leftTableView reloadData];
//        if (_dataSource && _dataSourceFlag.numberOfItemsInRow) {
////            [_rightTableView reloadData];
//        }
        [self animateIndicator:_indicators[touchIndex] background:_backGroundView tableView:_leftTableView title:_titles[touchIndex] reverse:YES complecte:^{
            weakSelf.isShow = YES;
        }];
        
        
        if (self.GetCurrenSelectColumnBlock) {
            self.GetCurrenSelectColumnBlock(touchIndex, YES);
        }
    }
    
}

-(void)showSelectView
{//展示筛选页面
    if (_allowShowSelectView) {
        [_selectView show];
    }
    
    
    
    
//    if (_showAnimation) {
//        [_selectView show];
//    }
//    else
//    {
//        _selectView.hidden = NO;
//    }
//
    
}

-(void)hiddenSelectView
{//隐藏筛选页面
    _shaiXuanViewHidden = YES;
    
    [_selectView dismiss:^(NSDictionary * _Nonnull resultDic) {
        NSLog(@"resultDic = %@",resultDic);
    }];
}

- (void)backTapped:(UITapGestureRecognizer *)gesture {
    YYMenuWeakSelf
    [self animateIndicator:_indicators[_currentSelectedColumn] background:_backGroundView tableView:_leftTableView title:_titles[_currentSelectedColumn] reverse:NO complecte:^{
        weakSelf.isShow = NO;
    }];
    
    if (_needCreateTopView) {
        UIView *view = [self viewWithTag:_currentSelectedColumn + 1];
        YYTopItemView *itemView = [view viewWithTag:10000];
        if ([itemView isKindOfClass:[YYTopItemView class]]) {
            itemView.selected = NO;
            
//            for (YYTopItemView *objectView in _itemArr) {
//                if (objectView != itemView) {
//                    objectView.selected = NO;
//                }
//            }
        }
    }
}

- (BOOL)setMenuWithSelectedRow:(NSInteger)row {
    _currentSelectedRows[_currentSelectedColumn] = @(row);
    YYMenuWeakSelf
    CATextLayer *title = (CATextLayer *)_titles[_currentSelectedColumn];
    if (_dataSourceFlag.numberOfItemsInRow && [_dataSource menu:self numberOfItemsInRow:row inColumn:_currentSelectedColumn] > 0) {
        if (_isClickHaveItemValid) {
            title.string = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:row]];
        }else {
        }
        return NO;
    }else {
        if (row == 0 && _currentSelectedColumn < _defaultColumnItemArr.count) {
            NSString *titleString = _defaultColumnItemArr[_currentSelectedColumn];
            if ([titleString isKindOfClass:[NSString class]] && titleString.length > 0) {
                title.string = titleString;
                [self animateIndicator:_indicators[_currentSelectedColumn] background:_backGroundView tableView:_leftTableView title:title reverse:NO complecte:^{
                    weakSelf.isShow = NO;
                }];
            }
            else
            {
                title.string = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:row]];
                [self animateIndicator:_indicators[_currentSelectedColumn] background:_backGroundView tableView:_leftTableView title:title reverse:NO complecte:^{
                    weakSelf.isShow = NO;
                }];
            }
            return YES;
        }
        title.string = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:row]];
        [self animateIndicator:_indicators[_currentSelectedColumn] background:_backGroundView tableView:_leftTableView title:title reverse:NO complecte:^{
            weakSelf.isShow = NO;
        }];
        return YES;
    }
}

- (void)setMenuWithSelectedItem:(NSInteger)item {
    
    
    
    if (_currentSelectedRows) {
        
        
        
        
    }
    
    
    
    CATextLayer *title = (CATextLayer *)_titles[_currentSelectedColumn];
    NSInteger currentSelectedMenudRow = [_currentSelectedRows[_currentSelectedColumn] integerValue];
    title.string = [_dataSource menu:self titleForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:currentSelectedMenudRow item:item]];
    YYMenuWeakSelf
    [self animateIndicator:_indicators[_currentSelectedColumn] background:_backGroundView tableView:_leftTableView title:title reverse:NO complecte:^{
        weakSelf.isShow = NO;
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end


























////
////  YYTopMenuView.m
////  NNHealthMall
////
////  Created by 蓓蕾 on 2021/8/20.
////  Copyright © 2021 YY. All rights reserved.
////
//
//#import "YYTopMenuView.h"
//
//#define YYMenuWeakSelf __weak typeof(self) weakSelf = self;
//
//#define YYMenu_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
//#define YYMenu_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
//#define YYMenu_kTableViewCellHeight 44
//#define YYMenu_kTextColor [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1]
//#define YYMenu_kDetailTextColor [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1]
//#define YYMenu_kSeparatorColor [UIColor colorWithRed:219/255.0 green:219/255.0 blue:219/255.0 alpha:1]
//#define YYMenu_kCellBgColor [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1]
//#define YYMenu_kTextSelectColor [UIColor colorWithRed:246/255.0 green:79/255.0 blue:0/255.0 alpha:1]
//#define YYMenu_kTableViewHeight 300
//
//typedef void(^complete)(void);
//
//#pragma mark - YYIndexPath
//@implementation YYIndexPath
//
//- (instancetype)initWithColumn:(NSInteger)column row:(NSInteger)row {
//    if (self = [super init]) {
//        _column = column;
//        _row = row;
//        _item = -1;
//    }
//    return self;
//}
//
//- (instancetype)initWithColumn:(NSInteger)column row:(NSInteger)row item:(NSInteger)item {
//    self = [self initWithColumn:column row:row];
//    if (self) {
//        _item = item;
//    }
//    return self;
//}
//
//+ (instancetype)indexPathWithColumn:(NSInteger)column row:(NSInteger)row {
//    YYIndexPath *path = [[self alloc] initWithColumn:column row:row];
//    return path;
//}
//
//+ (instancetype)indexPathWithColumn:(NSInteger)column row:(NSInteger)row item:(NSInteger)item {
//    return [[self alloc] initWithColumn:column row:row item:item];
//}
//
//@end
//
//#pragma mark - YYTopMenuView
//@interface YYTopMenuView () <UITableViewDataSource, UITableViewDelegate>
//{
//    struct {
//        unsigned int numberOfRowsInColumn :1;
//        unsigned int numberOfItemsInRow :1;
//        unsigned int titleForRowsAtIndexPath :1;
//        unsigned int titleForItemInRowAtIndexPath :1;
//        unsigned int imageNameForRowAtIndexPath :1;
//        unsigned int imageNameForItemInRowAtIndexPath :1;
//        unsigned int detailTextForRowAtIndexPath :1;
//        unsigned int detailTextForItemInRowAtIndexPath :1;
//    }_dataSourceFlag;
//}
//
//@property (nonatomic, assign) CGFloat tableViewHeight;
//@property (nonatomic, assign) CGPoint origin;  //原点
//@property (nonatomic, assign) CGFloat height;
//@property (nonatomic, assign) NSInteger numberOfColumn;  //列数
//@property (nonatomic, assign) BOOL isShow;
//@property (nonatomic, strong) UIView *backGroundView;
//@property (nonatomic, strong) UITableView *leftTableView;  //一级列表
//@property (nonatomic, strong) UITableView *rightTableView;  //二级列表
//@property (nonatomic, assign) NSInteger currentSelectedColumn;  //当前选中列
//@property (nonatomic, strong) UIView *bottomLine;  //底部的线条
//
////layers array
//@property (nonatomic, copy) NSArray *titles;
//@property (nonatomic, copy) NSArray *indicators;
//@property (nonatomic, copy) NSArray *bgLayers;
//
//@end
//
//
//@implementation YYTopMenuView
//
////初始化方法
//- (instancetype)initWithOrigin:(CGPoint)origin andHeight:(CGFloat)height {
//    self = [self initWithFrame:CGRectMake(origin.x, origin.y, YYMenu_SCREEN_WIDTH, height)];
//    if (self) {
//        _origin = origin;
//        _height = height;
//        _isShow = NO;
//        _fontSize = 14;
//        _currentSelectedColumn = -1;
//        _isClickHaveItemValid = YES;
//        _textColor = YYMenu_kTextColor;
//        _selectedTextColor = YYMenu_kTextSelectColor;
//        _detailTextFont = [UIFont systemFontOfSize:11];
//        _separatorColor = YYMenu_kSeparatorColor;
//        _detailTextColor = YYMenu_kDetailTextColor;
//        _indicatorColor = YYMenu_kTextColor;
//        _tableViewHeight = YYMenu_kTableViewHeight;
//
//        //初始化两个tableView【下面的列表】
//        _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x, origin.y + self.frame.size.height, YYMenu_SCREEN_WIDTH / 2, 0) style:UITableViewStylePlain];
//        _leftTableView.delegate = self;
//        _leftTableView.dataSource = self;
//        _leftTableView.rowHeight = YYMenu_kTableViewCellHeight;
//        _leftTableView.separatorColor = YYMenu_kSeparatorColor;
//
//        //【上面的item】
//        _rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(origin.x + YYMenu_SCREEN_WIDTH / 2, origin.y + self.frame.size.height, YYMenu_SCREEN_WIDTH / 2, 0) style:UITableViewStylePlain];
//        _rightTableView.delegate = self;
//        _rightTableView.dataSource = self;
//        _rightTableView.rowHeight = YYMenu_kTableViewCellHeight;
//        _rightTableView.separatorColor = YYMenu_kSeparatorColor;
//
////        _leftTableView.backgroundColor = [UIColor orangeColor];
////        _rightTableView.backgroundColor = [UIColor purpleColor];
//
//        self.backgroundColor = [UIColor whiteColor];
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuTapped:)];
//        [self addGestureRecognizer:tap];
//
//        _backGroundView = [[UIView alloc] init];
//        _backGroundView.frame = CGRectMake(origin.x, origin.y, YYMenu_SCREEN_WIDTH, YYMenu_SCREEN_HEIGHT);
//        _backGroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
//        _backGroundView.opaque = NO;
//        UITapGestureRecognizer *backTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTapped:)];
//        [_backGroundView addGestureRecognizer:backTap];
//
//        //底部线条
//        _bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 0.5, YYMenu_SCREEN_WIDTH, 0.5)];
//        _bottomLine.backgroundColor = YYMenu_kSeparatorColor;
//        _bottomLine.hidden = YES;
//        [self addSubview:_bottomLine];
//
//    }
//    return self;
//}
//
//#pragma mark - 懒加载
//- (UIColor *)indicatorColor {
//    if (!_indicatorColor) {
//        _indicatorColor = [UIColor blackColor];
//    }
//    return _indicatorColor;
//}
//
//- (UIColor *)textColor {
//    if (!_textColor) {
//        _textColor = [UIColor blackColor];
//    }
//    return _textColor;
//}
//
//- (UIColor *)separatorColor {
//    if (!_separatorColor) {
//        _separatorColor = [UIColor blackColor];
//    }
//    return _separatorColor;
//}
//
//#pragma mark - 设置dataSource
//- (void)setDataSource:(id<YYMenuDataSource>)dataSource {
//    if (_dataSource == dataSource) {
//        return;
//    }
//    _dataSource = dataSource;
//    if ([_dataSource respondsToSelector:@selector(numberOfColumnsInMenu:)]) {
//        _numberOfColumn = [_dataSource numberOfColumnsInMenu:self];
//    }else {
//        _numberOfColumn = 1;
//    }
//
//
//    _currentSelectedRows = [[NSMutableArray alloc] initWithCapacity:_numberOfColumn];
//    for (int i=0; i<_numberOfColumn; i++) {
//        [_currentSelectedRows addObject:@(0)];
//    }
//
//    //判断是否响应了某方法
//    _dataSourceFlag.numberOfRowsInColumn = [_dataSource respondsToSelector:@selector(menu:numberOfRowsInColumn:)];
//    _dataSourceFlag.numberOfItemsInRow = [_dataSource respondsToSelector:@selector(menu:numberOfItemsInRow:inColumn:)];
//    _dataSourceFlag.titleForRowsAtIndexPath = [_dataSource respondsToSelector:@selector(menu:titleForRowAtIndexPath:)];
//    _dataSourceFlag.titleForItemInRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:titleForItemsInRowAtIndexPath:)];
//    _dataSourceFlag.imageNameForRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:imageNameForRowAtIndexPath:)];
//    _dataSourceFlag.imageNameForItemInRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:imageForItemsInRowAtIndexPath:)];
//    _dataSourceFlag.detailTextForRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:detailTextForRowAtIndexPath:)];
//    _dataSourceFlag.detailTextForItemInRowAtIndexPath = [_dataSource respondsToSelector:@selector(menu:detailTextForItemsInRowAtIndexPath:)];
//
//    CGFloat numberOfLine = YYMenu_SCREEN_WIDTH / self.numberOfColumn;
//    CGFloat numberOfBackground = YYMenu_SCREEN_WIDTH / self.numberOfColumn;
//    CGFloat numberOfTextLayer = YYMenu_SCREEN_WIDTH / (self.numberOfColumn * 2);
//
//    //底部的line显示
//    _bottomLine.hidden = NO;
//
//    NSMutableArray *tempTitles = [[NSMutableArray alloc] initWithCapacity:_numberOfColumn];
//    NSMutableArray *tempIndicators = [[NSMutableArray alloc] initWithCapacity:_numberOfColumn];
//    NSMutableArray *tempBgLayers = [[NSMutableArray alloc] initWithCapacity:_numberOfColumn];
//
//
//    //画出菜单
//    for (int i = 0; i < _numberOfColumn; i++) {
//        //backgrounLayer
//        CGPoint positionForBackgroundLayer = CGPointMake((i + 0.5) * numberOfBackground, self.height / 2);
//        CALayer *bgLayer = [self createBackgroundLayerWithPosition:positionForBackgroundLayer color:[UIColor whiteColor]];
//        [self.layer addSublayer:bgLayer];
//        [tempBgLayers addObject:bgLayer];
//
//        //titleLayer
//        NSString *titleString = nil;
//        if (!_isClickHaveItemValid && [_dataSource menu:self numberOfItemsInRow:0 inColumn:i] > 0 && _dataSourceFlag.numberOfItemsInRow) {
//            titleString = [_dataSource menu:self titleForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:i row:0 item:0]];
//        }else {
//            titleString = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:i row:0]];
//        }
//        CGPoint positionForTitle = CGPointMake(( i * 2 + 1) * numberOfTextLayer, self.height / 2);
//        CATextLayer *textLayer = [self createTitleLayerWithString:titleString position:positionForTitle color:self.textColor];
//        [self.layer addSublayer:textLayer];
//        [tempTitles addObject:textLayer];
//
//
//        //indicatorLayer
//        CGSize size = [self calculateTitleSizeWithString:titleString];
//        CGPoint indicatorPosition = CGPointMake(textLayer.position.x+size.width/2+10, self.height / 2+2);
//        CAShapeLayer *sharpLayer = [self createIndicatorWithPosition:indicatorPosition color:self.indicatorColor];
//        [self.layer addSublayer:sharpLayer];
//        [tempIndicators addObject:sharpLayer];
//
//        //separatorLayer
//        if (i != self.numberOfColumn - 1) {
//            CGPoint separatorPosition = CGPointMake(ceilf((i + 1) * numberOfLine - 1), self.height / 2);
//            CAShapeLayer *separatorLayer = [self createSeparatorWithPosition:separatorPosition color:self.separatorColor];
//            [self.layer addSublayer:separatorLayer];
//        }
//    }
//    _titles = [tempTitles copy];
//    _indicators = [tempIndicators copy];
//    _bgLayers = [tempBgLayers copy];
//}
//
//#pragma mark - 绘图
////背景
//- (CALayer *)createBackgroundLayerWithPosition:(CGPoint)position color:(UIColor *)color {
//    CALayer *layer = [CALayer layer];
//    layer.position = position;
//    layer.bounds = CGRectMake(0, 0, YYMenu_SCREEN_WIDTH / self.numberOfColumn, self.height - 1);
//    layer.backgroundColor = color.CGColor;
//    return layer;
//}
////标题
//- (CATextLayer *)createTitleLayerWithString:(NSString *)string position:(CGPoint)position color:(UIColor *)color {
//    CGSize size = [self calculateTitleSizeWithString:string];
//
//    CATextLayer *layer = [CATextLayer new];
//    CGFloat sizeWidth = (size.width < (self.frame.size.width / _numberOfColumn) - 25) ? size.width : self.frame.size.width / _numberOfColumn - 25;
//    layer.bounds = CGRectMake(0, 0, sizeWidth, size.height);
//    layer.string = string;
//    layer.fontSize = _fontSize;
//    layer.alignmentMode = kCAAlignmentCenter;
//    layer.truncationMode = kCATruncationEnd;
//    layer.foregroundColor = color.CGColor;
//
//    layer.contentsScale = [[UIScreen mainScreen] scale];
//
//    layer.position = position;
//
//    return layer;
//}
////计算String的宽度
//- (CGSize)calculateTitleSizeWithString:(NSString *)string
//{
//    //CGFloat fontSize = 14.0;
//    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:_fontSize]};
//    CGSize size = [string boundingRectWithSize:CGSizeMake(280, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;
//    return CGSizeMake(ceilf(size.width)+2, size.height);
//}
//
////指示器
//- (CAShapeLayer *)createIndicatorWithPosition:(CGPoint)position color:(UIColor *)color {
//    CAShapeLayer *layer = [CAShapeLayer new];
//
//    UIBezierPath *path = [UIBezierPath new];
//    [path moveToPoint:CGPointMake(0, 0)];
//    [path addLineToPoint:CGPointMake(5, 5)];
//    [path moveToPoint:CGPointMake(5, 5)];
//    [path addLineToPoint:CGPointMake(10, 0)];
//    [path closePath];
//    layer.path = path.CGPath;
//    layer.lineWidth = 0.8;
//    layer.strokeColor = [UIColor blackColor].CGColor;
////    layer.fillColor = self.selectedTextColor.CGColor;
//
//
//    CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit);
//    layer.bounds = CGPathGetBoundingBox(bound);
//    CGPathRelease(bound);
//    layer.position = position;
//
//    return layer;
//}
////分隔线
//- (CAShapeLayer *)createSeparatorWithPosition:(CGPoint)position color:(UIColor *)color {
//    CAShapeLayer *layer = [CAShapeLayer new];
//
//    UIBezierPath *path = [UIBezierPath new];
//    [path moveToPoint:CGPointMake(160,0)];
//    [path addLineToPoint:CGPointMake(160, 20)];
//
//    layer.path = path.CGPath;
//    layer.lineWidth = 1;
//    layer.strokeColor = color.CGColor;
//
//
//    CGPathRef bound = CGPathCreateCopyByStrokingPath(layer.path, nil, layer.lineWidth, kCGLineCapButt, kCGLineJoinMiter, layer.miterLimit);
//    layer.bounds = CGPathGetBoundingBox(bound);
//    CGPathRelease(bound);
//    layer.position = position;
//    return layer;
//}
//
//#pragma mark - 动画
//- (void)animateIndicator:(CAShapeLayer *)indicator reverse:(BOOL)reverse complete:(complete)complete {
//    [CATransaction begin];
//    [CATransaction setAnimationDuration:0.25];
//    [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0.4 :0.0 :0.2 :1.0]];
//
//    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
//    anim.values = reverse ? @[ @0, @(M_PI) ] : @[ @(M_PI), @0 ];
//
//    if (!anim.removedOnCompletion) {
//        [indicator addAnimation:anim forKey:anim.keyPath];
//    } else {
//        [indicator addAnimation:anim forKey:anim.keyPath];
//        [indicator setValue:anim.values.lastObject forKeyPath:anim.keyPath];
//    }
//
//    [CATransaction commit];
//    if (reverse) {
//        indicator.strokeColor = self.selectedTextColor.CGColor;
//    }else {
//        indicator.strokeColor = self.textColor.CGColor;
//    }
//    complete();
//}
//
//- (void)animateBackGroundView:(UIView *)view show:(BOOL)show complete:(complete)complete {
//    if (show) {
//        [self.superview addSubview:view];
//        [view.superview addSubview:self];
//        [UIView animateWithDuration:0.2 animations:^{
//            view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
//        }];
//    }else {
//        [UIView animateWithDuration:0.2 animations:^{
//            view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
//        } completion:^(BOOL finished) {
//            if (finished) {
//                [view removeFromSuperview];
//            }
//        }];
//    }
//    complete();
//}
//
//- (void)animateTableView:(UITableView *)tableview show:(BOOL)show complete:(complete)complete {
//    BOOL haveItems = NO;
//    if (_dataSource) {
//        NSInteger num = [self.leftTableView numberOfRowsInSection:0];
//        for (int i=0; i<num; i++) {
//            if (_dataSourceFlag.numberOfItemsInRow && [_dataSource menu:self numberOfItemsInRow:i inColumn:self.currentSelectedColumn] > 0) {
//                haveItems = YES;
//                break;
//            }
//        }
//    }
//    YYMenuWeakSelf
//    if (show) {
//        if (haveItems) {
//            _leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH / 2, 0);
//            _rightTableView.frame = CGRectMake(self.origin.x + YYMenu_SCREEN_WIDTH / 2, self.origin.y +self.height, YYMenu_SCREEN_WIDTH / 2, 0);
//            [self.superview addSubview:_leftTableView];
//            [self.superview addSubview:_rightTableView];
//        }else {
//            _leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH, 0);
//            [self.superview addSubview:_leftTableView];
//        }
//        CGFloat num = [_leftTableView numberOfRowsInSection:0];
//        CGFloat tableViewHeight = num * YYMenu_kTableViewCellHeight > YYMenu_kTableViewHeight ? YYMenu_kTableViewHeight : num * YYMenu_kTableViewCellHeight;
//
//        [UIView animateWithDuration:0.2 animations:^{
//            if (haveItems) {
//                weakSelf.leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH / 2, tableViewHeight);
//                weakSelf.rightTableView.frame = CGRectMake(self.origin.x + YYMenu_SCREEN_WIDTH / 2, self.origin.y +self.height, YYMenu_SCREEN_WIDTH / 2, tableViewHeight);
//            }else {
//                weakSelf.leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH, tableViewHeight);
//            }
//        }];
//    }else {
//        [UIView animateWithDuration:0.2 animations:^{
//            if (haveItems) {
//                weakSelf.leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH / 2, 0);
//                weakSelf.rightTableView.frame = CGRectMake(self.origin.x + YYMenu_SCREEN_WIDTH / 2, self.origin.y +self.height, YYMenu_SCREEN_WIDTH / 2, 0);
//            }else {
//                weakSelf.leftTableView.frame = CGRectMake(self.origin.x, self.origin.y + self.height, YYMenu_SCREEN_WIDTH, 0);
//            }
//        } completion:^(BOOL finished) {
//            if (weakSelf.rightTableView) {
//                [weakSelf.rightTableView removeFromSuperview];
//            }
//            [weakSelf.leftTableView removeFromSuperview];
//        }];
//    }
//    complete();
//}
//
//- (void)animateTitle:(CATextLayer *)title Indicator:(CAShapeLayer *)indicator show:(BOOL)show complete:(complete)complete {
//    CGSize size = [self calculateTitleSizeWithString:title.string];
//    CGFloat sizeWidth = (size.width < (self.frame.size.width / _numberOfColumn) - 25) ? size.width : self.frame.size.width / _numberOfColumn - 25;
//    title.bounds = CGRectMake(0, 0, sizeWidth, size.height);
//    if (indicator) {
//        CGPoint indicatorPosition = CGPointMake(title.position.x+sizeWidth/2+10, self.height / 2+2);
//        indicator.position = indicatorPosition;
//    }
//    if (!show) {
//        title.foregroundColor = _textColor.CGColor;
//    } else {
//        title.foregroundColor = _selectedTextColor.CGColor;
//    }
//    complete();
//}
//
//- (void)animateIndicator:(CAShapeLayer *)indicator background:(UIView *)background tableView:(UITableView *)tableView title:(CATextLayer *)title reverse:(BOOL)reverse complecte:(void(^)(void))complete {
//
//    [self animateIndicator:indicator reverse:reverse complete:^{
//       [self animateTitle:title Indicator:indicator show:reverse complete:^{
//          [self animateBackGroundView:background show:reverse complete:^{
//             [self animateTableView:tableView show:reverse complete:^{
//
//             }];
//          }];
//       }];
//    }];
//    complete();
//}
//
//
//#pragma mark - UITableView的dataSource和delegate
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    if (tableView == _leftTableView) {
//        if (_dataSourceFlag.numberOfRowsInColumn) {
//            return [_dataSource menu:self numberOfRowsInColumn:_currentSelectedColumn];
//        }else {
//            return 0;
//        }
//    }else {
//        if (_dataSourceFlag.numberOfItemsInRow) {
//            NSInteger row = [_currentSelectedRows[_currentSelectedColumn] integerValue];
//            return [_dataSource menu:self numberOfItemsInRow:row inColumn:_currentSelectedColumn];
//        }else {
//            return 0;
//        }
//    }
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString *ID = @"cell";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
//        cell.textLabel.textColor = _textColor;
//        cell.textLabel.highlightedTextColor = _selectedTextColor;
//        cell.textLabel.font = [UIFont systemFontOfSize:_fontSize];
//        if (_dataSourceFlag.detailTextForRowAtIndexPath && _dataSourceFlag.detailTextForItemInRowAtIndexPath) {
//            cell.detailTextLabel.textColor = _detailTextColor;
//            cell.detailTextLabel.font = _detailTextFont;
//        }
//    }
//    if (tableView == _leftTableView) {
//        if (_dataSourceFlag.titleForRowsAtIndexPath) {
//            cell.textLabel.text = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row]];
//            if (_dataSourceFlag.imageNameForRowAtIndexPath) {
//                NSString *imgName = [_dataSource menu:self imageNameForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row]];
//                if (imgName && imgName.length > 0) {
//                    cell.imageView.image = [UIImage imageNamed:imgName];
//                }else {
//                    cell.imageView.image = nil;
//                }
//            }else {
//                cell.imageView.image = nil;
//            }
//
//            //detailText
//            if (_dataSourceFlag.detailTextForRowAtIndexPath) {
//                cell.detailTextLabel.text = [_dataSource menu:self detailTextForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row]];
//            }else {
//                cell.detailTextLabel.text = nil;
//            }
//        }
//        //设置accessory
//        NSInteger currentSelectRow = [_currentSelectedRows[_currentSelectedColumn] integerValue];
//
//        //NSLog(@"当前%ld栏中%ld列", _currentSelectedColumn, currentSelectRow);
//
//        if (indexPath.row == currentSelectRow) {
//            [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
//        }
//
//        if (_dataSourceFlag.numberOfItemsInRow && [_dataSource menu:self numberOfItemsInRow:indexPath.row inColumn:_currentSelectedColumn] > 0) {
//            cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory_normal"] highlightedImage:[UIImage imageNamed:@"accessory_highlight"]];
//        }else {
//            cell.accessoryView = nil;
//        }
//    }else {
//        //右边
//        if (_dataSourceFlag.titleForItemInRowAtIndexPath) {
//            NSInteger currentSelectedRow = [_currentSelectedRows[_currentSelectedColumn] integerValue];
//            cell.textLabel.text = [_dataSource menu:self titleForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:currentSelectedRow item:indexPath.row]];
//            if (_dataSourceFlag.imageNameForItemInRowAtIndexPath) {
//                NSString *imgName = [_dataSource menu:self imageForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:currentSelectedRow item:indexPath.row]];
//                if (imgName && imgName.length > 0) {
//                    cell.imageView.image = [UIImage imageNamed:imgName];
//                }else {
//                    cell.imageView.image = nil;
//                }
//            }else {
//                cell.imageView.image = nil;
//            }
//
//            if (_dataSourceFlag.detailTextForItemInRowAtIndexPath) {
//                cell.detailTextLabel.text = [_dataSource menu:self detailTextForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:currentSelectedRow item:indexPath.row]];
//            }else {
//                cell.textLabel.text = nil;
//            }
//        }
//        if ([cell.textLabel.text isEqualToString:[(CATextLayer *)_titles[_currentSelectedColumn] string]]) {
//            NSInteger currentSelectedRow = [_currentSelectedRows[_currentSelectedColumn] integerValue];
//            [_leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:currentSelectedRow inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//            [_rightTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
//        }
//
//        cell.accessoryView = nil;
//
//    }
//    return cell;
//}
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (tableView == _leftTableView) {
//        BOOL haveItem = [self setMenuWithSelectedRow:indexPath.row];
//        BOOL isClickHaveItemValid = self.isClickHaveItemValid ? YES :haveItem;
//        if (isClickHaveItemValid && _delegate &&[_delegate respondsToSelector:@selector(menu:didSelectRowAtIndexPath:)]) {
//            [_delegate menu:self didSelectRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:indexPath.row]];
//        }
//    }else {
//        [self setMenuWithSelectedItem:indexPath.item];
//        if (_delegate && [_delegate respondsToSelector:@selector(menu:didSelectRowAtIndexPath:)]) {
//            [_delegate menu:self didSelectRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:[_currentSelectedRows[_currentSelectedColumn] integerValue] item:indexPath.item]];
//        }
//    }
//}
//
//#pragma mark 解决cell分割线左侧留空的问题
//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//    // 下面这几行代码是用来设置cell的上下行线的位置
//    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
//        [cell setSeparatorInset:UIEdgeInsetsZero];
//    }
//
//    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
//        [cell setLayoutMargins:UIEdgeInsetsZero];
//    }
//
//    //按照作者最后的意思还要加上下面这一段，才能做到底部线控制位置，所以这里按stackflow上的做法添加上吧。
//    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
//        [cell setPreservesSuperviewLayoutMargins:NO];
//    }
//}
//
//#pragma mark - 方法实现
////默认选中的index
//- (void)selectDeafultIndexPath {
//    [self selectIndexPath:[YYIndexPath indexPathWithColumn:0 row:0]];
//}
////获取IndexPath所对应的字符串
//- (NSString *)titleForRowAtIndexPath:(YYIndexPath *)indexPath {
//    return [self.dataSource menu:self titleForRowAtIndexPath:indexPath];
//}
////菜单切换
//- (void)selectIndexPath:(YYIndexPath *)indexPath {
//    if (!_delegate || !_dataSource || ![_delegate respondsToSelector:@selector(menu:didSelectRowAtIndexPath:)]) {
//        return;
//    }
//
//    if ([_dataSource numberOfColumnsInMenu:self] <= indexPath.column || [_dataSource menu:self numberOfRowsInColumn:indexPath.column] <= indexPath.row) {
//        return;
//    }
//
//    CATextLayer *title = (CATextLayer *)_titles[indexPath.column];
//
//    if (indexPath.item < 0 ) {
//
//        if (!_isClickHaveItemValid && [_dataSource menu:self numberOfItemsInRow:indexPath.row inColumn:indexPath.column] > 0){
//            title.string = [_dataSource menu:self titleForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:indexPath.column row:indexPath.row]];
//                [_delegate menu:self didSelectRowAtIndexPath:[YYIndexPath indexPathWithColumn:indexPath.column row:indexPath.row item:0]];
//        }else {
//            title.string = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:indexPath.column row:indexPath.row]];
//            [_delegate menu:self didSelectRowAtIndexPath:indexPath];
//        }
//        if (_currentSelectedRows.count > indexPath.column) {
//            _currentSelectedRows[indexPath.column] = @(indexPath.row);
//        }
//        CGSize size = [self calculateTitleSizeWithString:title.string];
//        CGFloat sizeWidth = (size.width < (self.frame.size.width / _numberOfColumn) - 25) ? size.width : self.frame.size.width / _numberOfColumn - 25;
//        title.bounds = CGRectMake(0, 0, sizeWidth, size.height);
//    }else if ([_dataSource menu:self numberOfItemsInRow:indexPath.row inColumn:indexPath.column] > indexPath.column) {
//        title.string = [_dataSource menu:self titleForItemsInRowAtIndexPath:indexPath];
//        [_delegate menu:self didSelectRowAtIndexPath:indexPath];
//        if (_currentSelectedRows.count > indexPath.column) {
//            _currentSelectedRows[indexPath.column] = @(indexPath.row);
//        }
//        CGSize size = [self calculateTitleSizeWithString:title.string];
//        CGFloat sizeWidth = (size.width < (self.frame.size.width / _numberOfColumn) - 25) ? size.width : self.frame.size.width / _numberOfColumn - 25;
//        title.bounds = CGRectMake(0, 0, sizeWidth, size.height);
//    }
//
//}
////数据重载
//- (void)reloadData {
//    YYMenuWeakSelf
//    [self animateBackGroundView:_backGroundView show:NO complete:^{
//       [self animateTableView:nil show:NO complete:^{
//           weakSelf.isShow = NO;
//           id vc = self.dataSource;
//           self.dataSource = nil;
//           self.dataSource = vc;
//       }];
//    }];
//}
//
//- (void)menuTapped:(UITapGestureRecognizer *)gesture {
//    if (_dataSource == nil) {
//        return;
//    }
//    //触摸的地方的index
//    CGPoint touchPoint = [gesture locationInView:self];
//    NSInteger touchIndex = touchPoint.x / (YYMenu_SCREEN_WIDTH / self.numberOfColumn);
//    YYMenuWeakSelf
//    //将当前点击的column之外的column给收回
//    for (int i=0; i<_numberOfColumn; i++) {
//        if (i != touchIndex) {
//            [self animateIndicator:_indicators[i] reverse:NO complete:^{
//               [self animateTitle:weakSelf.titles[i] Indicator:nil show:NO complete:^{
//
//               }];
//            }];
//        }
//    }
//
//    if (touchIndex == _currentSelectedColumn && _isShow) {
//        //收回menu
//        [self animateIndicator:_indicators[touchIndex] background:_backGroundView tableView:_leftTableView title:_titles[touchIndex] reverse:NO complecte:^{
//            weakSelf.currentSelectedColumn = touchIndex;
//            weakSelf.isShow = NO;
//        }];
//    }else {
//        //弹出menu
//        _currentSelectedColumn = touchIndex;
//        [_leftTableView reloadData];
//        if (_dataSource && _dataSourceFlag.numberOfItemsInRow) {
//            [_rightTableView reloadData];
//        }
//        [self animateIndicator:_indicators[touchIndex] background:_backGroundView tableView:_leftTableView title:_titles[touchIndex] reverse:YES complecte:^{
//            weakSelf.isShow = YES;
//        }];
//    }
//
//}
//
//- (void)backTapped:(UITapGestureRecognizer *)gesture {
//    YYMenuWeakSelf
//    [self animateIndicator:_indicators[_currentSelectedColumn] background:_backGroundView tableView:_leftTableView title:_titles[_currentSelectedColumn] reverse:NO complecte:^{
//        weakSelf.isShow = NO;
//    }];
//
//}
//
//- (BOOL)setMenuWithSelectedRow:(NSInteger)row {
//    _currentSelectedRows[_currentSelectedColumn] = @(row);
//    YYMenuWeakSelf
//    CATextLayer *title = (CATextLayer *)_titles[_currentSelectedColumn];
//    if (_dataSourceFlag.numberOfItemsInRow && [_dataSource menu:self numberOfItemsInRow:row inColumn:_currentSelectedColumn] > 0) {
//        if (_isClickHaveItemValid) {
//            title.string = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:row]];
//            [self animateTitle:title Indicator:_indicators[_currentSelectedColumn] show:YES complete:^{
//                [weakSelf.rightTableView reloadData];
//            }];
//        }else {
//            [_rightTableView reloadData];
//        }
//        return NO;
//    }else {
//        title.string = [_dataSource menu:self titleForRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:row]];
//        [self animateIndicator:_indicators[_currentSelectedColumn] background:_backGroundView tableView:_leftTableView title:title reverse:NO complecte:^{
//            weakSelf.isShow = NO;
//        }];
//        return YES;
//    }
//}
//
//- (void)setMenuWithSelectedItem:(NSInteger)item {
//    CATextLayer *title = (CATextLayer *)_titles[_currentSelectedColumn];
//    NSInteger currentSelectedMenudRow = [_currentSelectedRows[_currentSelectedColumn] integerValue];
//    title.string = [_dataSource menu:self titleForItemsInRowAtIndexPath:[YYIndexPath indexPathWithColumn:_currentSelectedColumn row:currentSelectedMenudRow item:item]];
//    YYMenuWeakSelf
//    [self animateIndicator:_indicators[_currentSelectedColumn] background:_backGroundView tableView:_leftTableView title:title reverse:NO complecte:^{
//        weakSelf.isShow = NO;
//    }];
//}
//
///*
//// Only override drawRect: if you perform custom drawing.
//// An empty implementation adversely affects performance during animation.
//- (void)drawRect:(CGRect)rect {
//    // Drawing code
//}
//*/
//
//@end
